<script>
import { ref } from 'vue'
import qianfanService from '@/api/qianfanApi'

export default {
  name: 'CenterBottom',
  setup() {
    const isFullscreen = ref(false)
    const messages = ref([
      { type: 'ai', content: '您好！我是AI青枣助手，有什么可以帮您的吗？' }
    ])
    const userInput = ref('')
    const isLoading = ref(false)

    const toggleFullscreen = () => {
      isFullscreen.value = !isFullscreen.value
      if (isFullscreen.value) {
        document.documentElement.style.overflow = 'hidden'
      } else {
        document.documentElement.style.overflow = ''
      }
    }

    const sendMessage = async () => {
      const content = userInput.value.trim()
      if (!content || isLoading.value) return

      // 添加用户消息
      messages.value.push({ type: 'user', content: content })
      userInput.value = ''
      isLoading.value = true

      // 添加加载中的AI消息
      const loadingMessageIndex = messages.value.length
      messages.value.push({ type: 'ai', content: '思考中...', loading: true })

      scrollToBottom()

      try {
        // 构建消息历史
        const chatMessages = [
          {
            role: 'system',
            content: '你是一个专业的AI助手，回答要简洁明了、专业准确。'
          },
          ...messages.value
            .filter(msg => !msg.loading && msg.type === 'user')
            .slice(-10) // 只保留最近10条用户消息避免过长
            .map(msg => ({
              role: 'user',
              content: msg.content
            }))
        ]

        // 调用千帆API
        const response = await qianfanService.chat(chatMessages)

        // 更新AI回复
        if (response.choices && response.choices[0]) {
          messages.value[loadingMessageIndex] = {
            type: 'ai',
            content: response.choices[0].message.content
          }
        } else {
          throw new Error('API返回格式异常')
        }
      } catch (error) {
        console.error('AI对话错误:', error)
        messages.value[loadingMessageIndex] = {
          type: 'ai',
          content: `抱歉，我暂时无法回答这个问题。错误: ${error.message}`
        }
      } finally {
        isLoading.value = false
        scrollToBottom()
      }
    }

    const scrollToBottom = () => {
      setTimeout(() => {
        const chatContainer = document.querySelector('.chat-container')
        const fullscreenMessages = document.querySelector('.fullscreen-messages')
        if (chatContainer) {
          chatContainer.scrollTop = chatContainer.scrollHeight
        }
        if (fullscreenMessages) {
          fullscreenMessages.scrollTop = fullscreenMessages.scrollHeight
        }
      }, 100)
    }

    const handleKeydown = (e) => {
      if (e.key === 'Enter' && !e.shiftKey) {
        e.preventDefault()
        sendMessage()
      }
    }

    const adjustTextareaHeight = (event) => {
      const textarea = event.target
      textarea.style.height = 'auto'
      textarea.style.height = Math.min(textarea.scrollHeight, 80) + 'px'
    }

    const clearChat = () => {
      messages.value = [
        { type: 'ai', content: '您好！我是AI青枣助手，有什么可以帮您的吗？' }
      ]
    }

    return {
      isFullscreen,
      messages,
      userInput,
      isLoading,
      toggleFullscreen,
      sendMessage,
      handleKeydown,
      adjustTextareaHeight,
      clearChat
    }
  }
}
</script>

<template>
  <div class="center-bottom">
    <dv-border-box-8 :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']">
      <div class="content-wrapper">
        <div class="header">
          <div class="title-container">
            <dv-decoration3 class="decoration-left" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" />
            <dv-border-box-2 class="title-box" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']">
              <div class="title">AI问答助手</div>
            </dv-border-box-2>
            <dv-decoration3 class="decoration-right" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']" />
          </div>
        </div>

        <div class="chat-content">
          <!-- AI形象区域 -->
          <div class="ai-avatar-section">
            <div class="ai-avatar">
              <div class="qingzao-character" :class="{ 'thinking': isLoading }">
                <div class="head">
                  <div class="face">
                    <div class="eyes">
                      <div class="eye left-eye"></div>
                      <div class="eye right-eye"></div>
                    </div>
                    <div class="mouth"></div>
                  </div>
                </div>
                <div class="body"></div>
              </div>
            </div>
            <div class="ai-info">
              <div class="ai-name">青枣AI</div>
              <div class="ai-status">{{ isLoading ? '思考中...' : '在线 · 准备为您服务' }}</div>
            </div>
          </div>

          <!-- 对话区域 -->
          <div class="chat-section">
            <div class="chat-container">
              <div v-for="(message, index) in messages" :key="index" :class="['message', message.type]">
                <div class="message-content" :class="{ 'loading': message.loading }">
                  <span v-if="message.loading" class="loading-dots">
                    <span></span>
                    <span></span>
                    <span></span>
                  </span>
                  <template v-else>{{ message.content }}</template>
                </div>
              </div>
            </div>

            <!-- 输入区域 -->
            <div class="input-section">
              <div class="input-container">
                <textarea v-model="userInput" placeholder="请输入您的问题..." @keydown="handleKeydown"
                  @input="adjustTextareaHeight" rows="1" :disabled="isLoading"></textarea>
                <button class="send-btn" @click="sendMessage" :disabled="isLoading || !userInput.trim()">
                  <span v-if="isLoading">发送中...</span>
                  <span v-else>发送</span>
                </button>
              </div>
            </div>
          </div>
        </div>

        <!-- 操作按钮区域 -->
        <div class="action-buttons">
          <button class="clear-btn" @click="clearChat" title="清空对话">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor">
              <path
                d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" />
            </svg>
            清空
          </button>
          <button class="fullscreen-btn" @click="toggleFullscreen" title="全屏">
            <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor">
              <path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z" />
            </svg>
            全屏
          </button>
        </div>
      </div>
    </dv-border-box-8>

    <!-- 全屏模式 -->
    <div v-if="isFullscreen" class="fullscreen-overlay">
      <div class="fullscreen-backdrop" @click="toggleFullscreen"></div>
      <div class="fullscreen-content">
        <div class="fullscreen-header">
          <div class="fullscreen-title">AI问答助手 - 全屏模式</div>
          <div class="fullscreen-actions">
            <button class="action-btn" @click="clearChat">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
                <path
                  d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" />
              </svg>
              清空对话
            </button>
            <button class="close-btn" @click="toggleFullscreen">×</button>
          </div>
        </div>
        <div class="fullscreen-body">
          <div class="fullscreen-main">
            <div class="fullscreen-ai-section">
              <div class="fullscreen-avatar">
                <div class="qingzao-character large" :class="{ 'thinking': isLoading }">
                  <div class="head">
                    <div class="face">
                      <div class="eyes">
                        <div class="eye left-eye"></div>
                        <div class="eye right-eye"></div>
                      </div>
                      <div class="mouth"></div>
                    </div>
                  </div>
                  <div class="body"></div>
                </div>
              </div>
              <div class="fullscreen-ai-info">
                <div class="ai-name">青枣AI助手</div>
                <div class="ai-desc">{{ isLoading ? '正在思考中...' : '智能问答 · 实时响应 · 专业解答' }}</div>
              </div>
            </div>

            <div class="fullscreen-chat-area">
              <div class="fullscreen-messages">
                <div v-for="(message, index) in messages" :key="index" :class="['fullscreen-message', message.type]">
                  <div class="message-content" :class="{ 'loading': message.loading }">
                    <span v-if="message.loading" class="loading-dots">
                      <span></span>
                      <span></span>
                      <span></span>
                    </span>
                    <template v-else>{{ message.content }}</template>
                  </div>
                </div>
              </div>

              <div class="fullscreen-input">
                <textarea v-model="userInput" placeholder="请输入您的问题..." @keydown="handleKeydown"
                  @input="adjustTextareaHeight" rows="1" :disabled="isLoading"></textarea>
                <button class="send-btn large" @click="sendMessage" :disabled="isLoading || !userInput.trim()">
                  {{ isLoading ? '发送中...' : '发送' }}
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.center-bottom {
  width: 100%;
  height: 100%;
  padding: 12px;
  position: relative;
}

.content-wrapper {
  width: 100%;
  height: 100%;
  background: rgba(0, 28, 36, 0.5);
  padding: 12px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  position: relative;
}

.header {
  margin-bottom: 10px;
}

.title-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.title-box {
  width: fit-content;
  height: 40px;
  padding: 0 30px;
}

.title {
  color: #00ffd9;
  font-size: 20px;
  font-weight: bold;
  white-space: nowrap;
  padding: 0 10px;
  line-height: 40px;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.decoration-left,
.decoration-right {
  width: 180px;
  height: 35px;
}

/* 聊天内容布局 */
.chat-content {
  flex: 1;
  display: flex;
  gap: 15px;
  height: calc(100% - 50px);
}

/* AI形象区域 */
.ai-avatar-section {
  width: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.ai-avatar {
  width: 80px;
  height: 120px;
  position: relative;
}

/* 青枣角色设计 */
.qingzao-character {
  width: 100%;
  height: 100%;
  position: relative;
}

.qingzao-character.large {
  transform: scale(1.5);
}

.head {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #a8e6cf, #7bcfa6);
  border-radius: 50% 50% 40% 40%;
  position: absolute;
  top: 0;
  left: 10px;
  border: 2px solid #5bbf95;
}

.body {
  width: 70px;
  height: 50px;
  background: linear-gradient(135deg, #7bcfa6, #5bbf95);
  border-radius: 30% 30% 50% 50%;
  position: absolute;
  bottom: 0;
  left: 5px;
  border: 2px solid #4aa882;
}

.face {
  position: absolute;
  top: 15px;
  left: 10px;
  width: 40px;
  height: 30px;
}

.eyes {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.eye {
  width: 8px;
  height: 8px;
  background: #2c5544;
  border-radius: 50%;
  animation: blink 3s infinite;
}

.mouth {
  width: 16px;
  height: 6px;
  background: #2c5544;
  border-radius: 0 0 8px 8px;
  margin: 0 auto;
}

@keyframes blink {

  0%,
  90%,
  100% {
    transform: scaleY(1);
  }

  95% {
    transform: scaleY(0.1);
  }
}

.ai-info {
  margin-top: 10px;
  text-align: center;
}

.ai-name {
  color: #00ffd9;
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 4px;
}

.ai-status {
  color: rgba(0, 255, 217, 0.7);
  font-size: 12px;
}

/* 对话区域 */
.chat-section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* 隐藏滚动条样式 */
.chat-container {
  flex: 1;
  background: rgba(0, 20, 28, 0.6);
  border-radius: 8px;
  padding: 10px;
  overflow-y: auto;
  margin-bottom: 10px;
  border: 1px solid rgba(0, 255, 217, 0.2);
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.chat-container::-webkit-scrollbar {
  display: none;
}

.message {
  margin-bottom: 12px;
  display: flex;
}

.message.ai {
  justify-content: flex-start;
}

.message.user {
  justify-content: flex-end;
}

.message-content {
  max-width: 80%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.4;
  word-break: break-word;
}

.message.ai .message-content {
  background: rgba(0, 255, 217, 0.1);
  border: 1px solid rgba(0, 255, 217, 0.3);
  color: #00ffd9;
}

.message.user .message-content {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #fff;
}

/* 输入区域 */
.input-section {
  border-top: 1px solid rgba(0, 255, 217, 0.2);
  padding-top: 10px;
}

.input-container {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.input-container textarea {
  flex: 1;
  background: rgba(0, 20, 28, 0.8);
  border: 1px solid rgba(0, 255, 217, 0.3);
  border-radius: 6px;
  color: #fff;
  padding: 8px 12px;
  font-size: 13px;
  resize: none;
  min-height: 36px;
  max-height: 80px;
  font-family: inherit;
  line-height: 1.4;
  /* 隐藏滚动条 */
  scrollbar-width: none;
  -ms-overflow-style: none;
  overflow-y: auto;
}

.input-container textarea::-webkit-scrollbar {
  display: none;
}

.input-container textarea:focus {
  outline: none;
  border-color: #00ffd9;
  box-shadow: 0 0 5px rgba(0, 255, 217, 0.3);
}

.input-container textarea::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.send-btn {
  background: linear-gradient(135deg, #00ffd9, #00ccaa);
  border: none;
  border-radius: 6px;
  color: #0a1a35;
  padding: 8px 16px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  height: 36px;
  flex-shrink: 0;
}

.send-btn:hover {
  background: linear-gradient(135deg, #00ccaa, #00a088);
  transform: translateY(-1px);
}

.send-btn.large {
  padding: 10px 20px;
  font-size: 14px;
  height: auto;
}

/* 全屏按钮 */
.fullscreen-btn {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 32px;
  height: 32px;
  background: rgba(0, 255, 217, 0.1);
  border: 1px solid rgba(0, 255, 217, 0.3);
  border-radius: 4px;
  color: #00ffd9;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.fullscreen-btn:hover {
  background: rgba(0, 255, 217, 0.2);
  transform: scale(1.1);
}

/* 全屏模式 */
.fullscreen-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.fullscreen-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.95);
}

.fullscreen-content {
  width: 95%;
  height: 95%;
  background: rgba(0, 28, 36, 0.95);
  border: 2px solid #00ffd9;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 1;
  box-shadow: 0 0 50px rgba(0, 255, 217, 0.2);
}

.fullscreen-header {
  padding: 20px 30px;
  border-bottom: 1px solid rgba(0, 255, 217, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(0, 20, 28, 0.8);
  flex-shrink: 0;
}

.fullscreen-title {
  color: #00ffd9;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.close-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}

.fullscreen-body {
  flex: 1;
  display: flex;
  padding: 30px;
  overflow: hidden;
}

.fullscreen-main {
  flex: 1;
  display: flex;
  gap: 30px;
  overflow: hidden;
}

.fullscreen-ai-section {
  width: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 30px;
  background: rgba(0, 20, 28, 0.6);
  border-radius: 12px;
  border: 1px solid rgba(0, 255, 217, 0.2);
  flex-shrink: 0;
}

.fullscreen-avatar {
  width: 80px;
  height: 115px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.fullscreen-ai-info {
  text-align: center;
}

.fullscreen-ai-info .ai-name {
  font-size: 24px;
  margin-bottom: 8px;
  color: #00ffd9;
}

.ai-desc {
  color: rgba(0, 255, 217, 0.8);
  font-size: 16px;
  line-height: 1.4;
}

.fullscreen-chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-width: 0;
}

.fullscreen-messages {
  flex: 1;
  background: rgba(0, 20, 28, 0.6);
  border-radius: 12px;
  padding: 20px;
  overflow-y: auto;
  border: 1px solid rgba(0, 255, 217, 0.2);
  scrollbar-width: none;
  -ms-overflow-style: none;
  min-height: 0;
}

.fullscreen-messages::-webkit-scrollbar {
  display: none;
}

.fullscreen-message {
  margin-bottom: 16px;
  display: flex;
}

.fullscreen-message.ai {
  justify-content: flex-start;
}

.fullscreen-message.user {
  justify-content: flex-end;
}

.fullscreen-message .message-content {
  max-width: 70%;
  padding: 12px 18px;
  border-radius: 16px;
  font-size: 16px;
  line-height: 1.5;
}

.fullscreen-input {
  display: flex;
  gap: 15px;
  align-items: flex-end;
  flex-shrink: 0;
}

.fullscreen-input textarea {
  flex: 1;
  background: rgba(0, 20, 28, 0.8);
  border: 1px solid rgba(0, 255, 217, 0.3);
  border-radius: 8px;
  color: #fff;
  padding: 12px 16px;
  font-size: 16px;
  resize: none;
  min-height: 60px;
  max-height: 120px;
  font-family: inherit;
  line-height: 1.4;
  /* 隐藏滚动条 */
  scrollbar-width: none;
  -ms-overflow-style: none;
  overflow-y: auto;
}

.fullscreen-input textarea::-webkit-scrollbar {
  display: none;
}

.fullscreen-input textarea:focus {
  outline: none;
  border-color: #00ffd9;
  box-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.fullscreen-input textarea::placeholder {
  color: rgba(255, 255, 255, 0.5);
}
</style>